package gbackup.command;

import gbackup.core.RemoteFile;
import gbackup.core.Shell;
import gbackup.core.Utility;
import gbackup.error.NoSuchDirectoryException;


public class LocalListCommand extends Command {
	public LocalListCommand(Shell shell, String rawCommand) {
		super(shell, rawCommand);
	}

	@Override
	public void execute() {
		switch (this._arguments.size()) {
		case 0:
			this._execute(this._shell.getLocalWorkingDirectory());
			break;
		case 1:
			String targetDirectoryPath = this._arguments.get(0);
			try {
			java.io.File targetDirectory = Utility.getDirectoryByPath(this._shell.getLocalRootDirectory(), this._shell.getLocalWorkingDirectory(), targetDirectoryPath);
			this._execute(targetDirectory);
			} catch (NoSuchDirectoryException nsde) {
				this._outputPrintStream.println("No Such Directory : [" + targetDirectoryPath + "]");
			}
			break;
		default:
			this._outputPrintStream.println("Invalid Argument : [" + this._rawCommand + "]");
			return;
		}
	}

	private void _execute(java.io.File workingDirectory) {
		for (java.io.File f : workingDirectory.listFiles()) {
			if (f.isDirectory())
				this._outputPrintStream.println(f.getName() + RemoteFile.pathSeperator);
			else
				this._outputPrintStream.println(f.getName());
		}
	}
}
